package revendication

import grails.test.*

class PostIntegrationTests extends GrailsUnitTestCase {
	
	def postService
	
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testFirstAdd() {
		def user = new User(login:'testuser',password:'jvuillermett')
		assertNotNull user.save()
		assertNotNull user.id
		
		def post1 = new Post(title:'testPost',content:'this is a  test post')
		def post2 = new Post(title:'testPost2',content:'this is a second test post')
		user.addToPosts(post1)
		user.addToPosts(post2)
		
		def posts = postService.getPostsFor("testuser")
		
		assertEquals posts.size(), 2
    }
	
	void testVotePost() {
		def user = new User(login:'testuser',password:'jvuillermett')
		def user2 = new User(login:'testuser2',password:'jvuillermett')
		def post1 = new Post(title:'testPost',content:'this is a  test post')
		def post2 = new Post(title:'testPost2',content:'this is a second test post')
		assertNotNull user.save()
		user.addToPosts(post1)
		user.addToPosts(post2)
		
		user.addToLikedPosts(post1)
		//post1.addToLikeUsers(user)
		//post1.save()
		
		def testpost = Post.findById(post1.id)
		def testuser = User.findById(user.id)
		assertEquals post1.likeUsers.size(), 1
		assertEquals testuser.likedPosts.size(), 1
		
	}
	
	void testDeleteUserAndPost() {
		def user = new User(login:'testuser',password:'jvuillermett')
		def user2 = new User(login:'testuser2',password:'jvuillermett')
		def post1 = new Post(title:'testPost',content:'this is a  test post')
		def post2 = new Post(title:'testPost2',content:'this is a second test post')
		assertNotNull user.save()
		assertNotNull user2.save()
		user.addToPosts(post1)
		user.addToPosts(post2)
		user2.addToLikedPosts(post1)
		user2.addToLikedPosts(post2)
		
		
		user2.delete()
		assertEquals 5, Post.list().size()
		user.delete()
		Post.list().each {println it.title }
		
		assertEquals 3, Post.list().size()
		//assertNotNull Post.findById(post1.id)
		
	}
}
